<script>
	import Vue from 'vue';
	import updateManager from './common/utils/update.js';
	import {
		API_BASE_URL
	} from './common/config.js';
	export default {
		onLaunch: function() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			});
			// #ifdef APP-PLUS
			// 锁定屏幕显示方向为竖屏正方向
			plus.screen.lockOrientation('portrait-primary');
			updateManager(API_BASE_URL + global.apiUrls.publicUpdateAPP);
			// #endif
		},
		onShow: function() {
			console.log('APP Show');
			// #ifdef APP-PLUS
			const args = plus.runtime.arguments;
			if (args) {
				let params_str = args.split('?')[1];
				if (params_str) {
					let params = params_str.split('&').reduce(function(total, item) {
						let arr = item.split('=');
						total[arr[0]] = arr[1];
						return total;
					}, {});
					uni.navigateTo({
						url: `${params.path}?goods_id=${params.goods_id}&activity_id=${params.activity_id}&sku_id=${params.sku_id}`
					})
				}
			}
			// #endif
		},
		onHide: function() {
			console.log('APP Hide');
		},
		methods: {
			updateApp() {
				plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
					console.log('widgetInfo', widgetInfo);
					this.$api.post(global.apiUrls.publicUpdateAPP, {
						app_ident: 'user'
					}).then(res => {
						console.log("res.data", res.data);
						if (res.data.code == 1 && res.data.data.is_take == 1) {
							if (res.data.data.version > widgetInfo.versionCode) {
								let apkUrl = res.data.data.url || '';
								uni.downloadFile({
									url: apkUrl,
									success: (downloadResult) => {
										if (downloadResult.statusCode === 200) {
											if (res.data.data.type == 1) {
												plus.runtime.install(downloadResult.tempFilePath, {
													force: true
												});
											} else {
												plus.nativeUI.confirm('新版本已下载,是否安装?', {
													title: '更新提示',
													buttons: ['取消', '安装']
												}, (result) => {
													if (result.index = 0) {
														plus.runtime.install(downloadResult.tempFilePath, {
															force: true
														});
														plus.runtime.restart();
													}
												})
											}
										}
									}
								});
							}
						}
					})
				});
			},
			checkPermissions() {
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android') { // 判断是Android
					var main = plus.android.runtimeMainActivity();
					var pkName = main.getPackageName();
					var uid = main.getApplicationInfo().plusGetAttribute("uid");
					var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
					var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
					// 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
					if (!areNotificationsEnabled) {
						uni.showModal({
							title: '通知权限开启提醒',
							content: '您还没有开启通知权限，无法接受到消息通知，是否前往设置？',
							success: function(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass('android.content.Intent');
									var Build = plus.android.importClass("android.os.Build");
									//android 8.0引导
									if (Build.VERSION.SDK_INT >= 26) {
										var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
										intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
									} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
										var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
										intent.putExtra("app_package", pkName);
										intent.putExtra("app_uid", uid);
									} else { //(<21)其他--跳转到该应用管理的详情页
										intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
										var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
										intent.setData(uri);
									}
									// 跳转到该应用的系统通知设置页
									main.startActivity(intent);
								}
							}
						});
					}
				} else if (plus.os.name == 'iOS') { // 判断是ISO
					var isOn = undefined;
					var types = 0;
					var app = plus.ios.invoke('UIApplication', 'sharedApplication');
					var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
					if (settings) {
						types = settings.plusGetAttribute('types');
						plus.ios.deleteObject(settings);
					} else {
						types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
					}
					plus.ios.deleteObject(app);
					isOn = (0 != types);
					if (isOn == false) {
						uni.showModal({
							title: '通知权限开启提醒',
							content: '您还没有开启通知权限，无法接受到消息通知，是否前往设置？',
							success: function(res) {
								if (res.confirm) {
									var app = plus.ios.invoke('UIApplication', 'sharedApplication');
									var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
									plus.ios.invoke(app, 'openURL:', setting);
									plus.ios.deleteObject(setting);
									plus.ios.deleteObject(app);
								}
							}
						});
					}
				}
				// #endif
			}
		}
	}
</script>

<style>
	/* #ifndef APP-PLUS-NVUE */
	@import "./style/icon.css";
	@import "./style/main.css";
	@import "./style/animation.css";
	@import "./style/iconfont.css";

	/*每个页面公共css */
	@import './static/iconfont/iconfont.css';
	/* #endif */

	/* #ifdef H5 */
	uni-toast {
		z-index: 999999999;
	}

	uni-toast .uni-sample-toast {
		z-index: 999999999;
	}

	checkbox .uni-checkbox-input {
		width: 38upx;
		height: 38upx;
		border-radius: 50%;
	}

	checkbox .uni-checkbox-input.uni-checkbox-input-checked {
		border: 1px solid #F02523;
		background: #F02523;
		color: #fff !important;
		border-color: #FFFFFF !important;
	}

	uni-checkbox:not([disabled]) .uni-checkbox-input:hover {
		border-color: #CCCCCC !important;
	}

	checkbox .uni-checkbox-input.uni-checkbox-input-checked:before {
		font-size: 18px;
	}

	radio .uni-radio-input {
		width: 38upx;
		height: 38upx;
		border-radius: 50%;
	}

	radio .uni-radio-input.uni-radio-input-checked {
		border: 1px solid #FE922B;
		background: #FE922B;
		color: #fff !important;
	}

	radio .uni-radio-input.uni-radio-input-checked:before {
		font-size: 18px;
	}

	/* #endif */
	/* #ifndef APP-PLUS-NVUE */
	checkbox .wx-checkbox-input {
		width: 38upx;
		height: 38upx;
		border-radius: 50%;
	}

	checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		border: 1px solid #F02523;
		background: #F02523;
		color: #fff !important;
	}

	checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
		font-size: 18px;
	}

	radio .wx-radio-input {
		width: 38upx;
		height: 38upx;
		border-radius: 50%;
	}

	.uni-input-input {
		color: #333;
	}

	radio .wx-radio-input.wx-radio-input-checked {
		border: 1px solid #F02523;
		background: #F02523;
		color: #fff !important;
	}

	radio .wx-radio-input.wx-radio-input-checked::before {
		font-size: 18px;
	}

	page {
		background: #FFFFFF;
		height: 100%;
		color: #333333;
		font-size: 32rpx;
	}
	.row1 {
		overflow: hidden;    
		text-overflow:ellipsis;    
		whitewhite-space: nowrap;
	}
	.row2 {
		display: -webkit-box;    
		-webkit-box-orient: vertical;    
		-webkit-line-clamp: 2;    
		overflow: hidden;
	}
	.row3 {
		display: -webkit-box;    
		-webkit-box-orient: vertical;    
		-webkit-line-clamp: 3;    
		overflow: hidden;
	}

	/* #endif */
</style>
